package it.polimi.TravelDream.web.beans;

import java.util.ArrayList;
import java.util.List;

import it.polimi.TravelDream.ejb.dtos.FlightDTO;
import it.polimi.TravelDream.ejb.dtos.HotelDTO;
import it.polimi.TravelDream.ejb.dtos.PackageDTO;
import it.polimi.TravelDream.ejb.dtos.RoomDTO;
import it.polimi.TravelDream.ejb.eaos.UserMgr;
import it.polimi.TravelDream.ejb.searchManager.PersonalizationMgr;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "PersonalizeMgr")
@SessionScoped
public class PersonalizationMgrBean {

	@EJB
	private PersonalizationMgr pmgr;
	@EJB
	private UserMgr userMgr;
	private List<HotelDTO> currentHotelList;
	private List<String> currentHotelNameList;
	private List<RoomDTO> currentRoomList=new ArrayList<RoomDTO>();
	private String currentHotel;
	private int beds; 
	private List<FlightDTO> currentFlights = new ArrayList<FlightDTO>();
	private PackageDTO currentPackage = new PackageDTO();
	private List<FlightDTO> selectedFlightDTO = new ArrayList<FlightDTO>();
	
	
	
	public PersonalizationMgrBean(){};
	
	public String goToPersonalization(PackageDTO packageDTO){

		this.currentPackage = packageDTO;

		this.setCurrentPackage(packageDTO);

		this.pmgr.setSelectedPackage(packageDTO);
		this.currentHotelList = new ArrayList<HotelDTO>();
		this.pmgr.searchHotel();
		this.currentHotelList=this.pmgr.getHotelResultList();
		generateHotelNameList();
		return "personalization.xhtml?faces-redirect=true";
	}

	public void setHotel(){
		for(HotelDTO h:this.currentHotelList){
			if(h.getName().equals(currentHotel))
				this.pmgr.setSelectedHotel(h);
		}
	}
	
	public String goToRecap(){
		this.pmgr.createUserPackage(this.userMgr.getUserDTO());
		return "recap?faces-redirect=true";
	}
	
	public String goToHome() {
		return "home?faces-redirect=true";
	}
	
	public String seeBookedFlight() {
		return "detail flights booked?faces-redirect=true";
	}
	
	public String goBack() {
		return "flight personalization?faces-redirect=true";
	}
	

	public String proceedToRooms(){
		return "rooms personalization?faces-redirect=true";
		
	}
	
	public  String setRoom(){
		if(this.pmgr.selectRoom(beds)){
		this.currentRoomList = this.pmgr.getRoomsResultList();
		return "rooms personalization 2?faces-redirect=true";
		}
		else {
			return "error room personalization?faces-redirect=true";
		}
		
	}
	
	public String error(){
		return "rooms personalization 2?faces-redirect=true";
	}
	
	public String proceed() {
		this.pmgr.setEmptyFlightList();
		this.currentFlights = new ArrayList<FlightDTO>();
		for(FlightDTO f: this.currentPackage.getFlights()) {
			if(f.getDepartureDate().equals(this.pmgr.getArrivalDate()) || f.getDepartureDate().equals(pmgr.getDepartureDate())) {
				if(f.getDepartureCity().equals(this.pmgr.getDepartureCity()) || f.getArrivalCity().equals(this.pmgr.getDepartureCity())) {
					this.currentFlights.add(f);
				}
			}

		}
		return "flight personalization?faces-redirect=true";
	}
	
	public void addFlight(FlightDTO flightDTO) {
		if(!selectedFlightDTO.contains(flightDTO)){
			this.selectedFlightDTO.add(flightDTO);
			this.pmgr.addFlight(flightDTO);
		}
	}

	public void removeFlight(FlightDTO flightDTO){
		if(selectedFlightDTO.contains(flightDTO)){
			this.selectedFlightDTO.remove(flightDTO);
			this.pmgr.removeFlight(flightDTO);
		}

	}

	public PersonalizationMgr getPmgr() {
		return pmgr;
	}

	public void setPmgr(PersonalizationMgr pmgr) {
		this.pmgr = pmgr;
	}

	public List<HotelDTO> getCurrentHotelList() {
		return currentHotelList;
	}

	public void setCurrentHotelList(List<HotelDTO> currentHotelList) {
		this.currentHotelList = currentHotelList;
	}

	public List<String> getCurrentHotelNameList() {
		return currentHotelNameList;
	}

	public void setCurrentHotelNameList(List<String> currentHotelNameList) {
		this.currentHotelNameList = currentHotelNameList;
	}

	public String getCurrentHotel() {
		return currentHotel;
	}

	public void setCurrentHotel(String currentHotel) {
		this.currentHotel = currentHotel;
	}

	public List<FlightDTO> getCurrentArrivalFlight() {
		return currentFlights;
	}

	public void setCurrentArrivalFlight(List<FlightDTO> currentArrivalFlight) {
		this.currentFlights = currentArrivalFlight;
	}

	private void generateHotelNameList(){
		this.currentHotelNameList = new ArrayList<String>();
		for(HotelDTO h:this.currentHotelList){
			this.currentHotelNameList.add(h.getName());
		}
	}

	public List<FlightDTO> getCurrentFlights() {
		return currentFlights;
	}

	public void setCurrentFlights(List<FlightDTO> currentFlights) {
		this.currentFlights = currentFlights;
	}

	public List<FlightDTO> getSelectedFlightDTO() {
		return selectedFlightDTO;
	}

	public void setSelectedFlightDTO(List<FlightDTO> selectedFlightDTO) {
		this.selectedFlightDTO = selectedFlightDTO;
	}

	public PackageDTO getCurrentPackage() {
		return currentPackage;
	}

	public void setCurrentPackage(PackageDTO currentPackage) {
		this.currentPackage = currentPackage;
	}

	public List<RoomDTO> getCurrentRoomList() {
		return currentRoomList;
	}

	public void setCurrentRoomList(List<RoomDTO> currentRoomList) {
		this.currentRoomList = currentRoomList;
	}

	public int getBeds() {
		return beds;
	}

	public void setBeds(int beds) {
		this.beds = beds;
	}
}